package com.jhhc.RHDB.Data;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

import com.jhhc.FloodForecast.Sensor.Pojo.BDMS_ST_SENSOR_B;
import com.jhhc.RHDB.Data.Pojo.*;
import com.jhhc.FloodForecast.Sensor.Service.BDMS_ST_SENSORALARM_BService;
import com.jhhc.FloodForecast.Sensor.Service.BDMS_ST_SENSOR_BService;
import com.jhhc.StormSurgeForecast.AstronomicalTide.Pojo.TideParame;
import com.jhhc.utils.ToTimeUtil;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.jhhc.utils.GetDataService;
import com.jhhc.utils.JsonResult;
import io.swagger.annotations.ApiOperation;


@Api(value="RHDBcontroller",tags={"实况数据服务"})
@RequestMapping(value = "RHDB",headers = "Accept=application/json")
@RestController
public class RHDBController {
    @Autowired
    private BDMS_ST_SENSOR_BService bDMS_ST_SENSOR_BService;

    @Autowired
    private BDMS_ST_SENSORALARM_BService BDMS_ST_SENSORALARM_BService;
    @Autowired
    private GetDataService GetDataService;
    @Autowired
    private ToTimeUtil toTimeUtil;
    // 查询实况数据加站类
    @ApiOperation("查询实况数据")
    @RequestMapping(value = "Data/GetData", headers = "Accept=application/json", method = RequestMethod.GET)
    public JsonResult GetData_GET(@RequestParam Integer PID, @RequestParam List<String> STCDs,
                                  @RequestParam(required = false) String STTP, @RequestParam(required = false) List<SensorType> sensorType,
                                  @RequestParam StepLength steplength, @RequestParam String StartTM, @RequestParam String EndTM) {
        JsonResult jsonResult = new JsonResult();
        List<Stations> stationsList = new ArrayList<>();
        //第一步获取大的要素
        List<String> sensor_T = GetDataService.getSensor(STTP, sensorType);
//        System.out.println("sensor_T"+sensor_T);
        // 取对应站码 传感器表 下的数据
        // 1.根据站点去创建一个站网集合
        //List<BDMS_ST_SENSOR_B> sensor_bList = bDMS_ST_SENSOR_BService.selectBySTCD(PID, STCDs);
        List<BDMS_ST_SENSOR_B> sensor_bList = bDMS_ST_SENSOR_BService.selectBySTCD(PID, STCDs);
        // 根据站码分组
        Map<String, List<BDMS_ST_SENSOR_B>> StcdListGroup = sensor_bList.stream()
                .collect(Collectors.groupingBy(BDMS_ST_SENSOR_B::getStcd, Collectors.toList()));
        String stnm = null;

//        System.out.println("sensor_bList"+sensor_bList);
        for (Map.Entry<String, List<BDMS_ST_SENSOR_B>> StcdList : StcdListGroup.entrySet()) {
            Stations stations = new Stations();
            if ((sensorType != null && STTP!=null)||(sensorType == null && STTP!=null)) {  //传感器为空 站类不为空 sensorType != null && STTP!=null

                for (int j = 0; j < StcdList.getValue().size(); j++) { // 获取循环分组后的数据
                    for (String  tp:sensor_T){
                        if (tp.equals(StcdList.getValue().get(j).getSensortps())){

                            Sensors sensors = new Sensors();

                            sensors.setSensorName(StcdList.getValue().get(j).getSensornm()); // 传感器名称
                            sensors.setSensorType(SensorType.valueOf(StcdList.getValue().get(j).getSensortps())); // 传感器类型
                            sensors.setDatacode(StcdList.getValue().get(j).getDatacode()); // 传感器取数码
                            List<Data> data = null;
                            sensors.setDataList(data);
                            // 站名
                            stnm = StcdList.getValue().get(j).getStnm();
                            stations.setSensors(sensors);
                        }
                    }
                }
            } else if(sensorType != null){
                //StcdList.getValue().forEach(System.out::println);
                for (SensorType type : sensorType) {
                    if (StcdList.getValue()!=null&&StcdList.getValue().size()>0){
                       // System.out.println("tp"+type);
                        BDMS_ST_SENSOR_B b = StcdList.getValue().stream().filter(bdmsStSensorB -> bdmsStSensorB.getSensortps().equals(type.toString())).findAny().orElse(null);
                        if (b!=null){
                            Sensors sensors = new Sensors();
                            // 传感器名称
                            sensors.setSensorName(b.getSensornm());
                            // 传感器类型
                            sensors.setSensorType(SensorType.valueOf(b.getSensortps()));
                            // 传感器取数码
                            sensors.setDatacode(b.getDatacode());
                            // 站名
                            stnm = b.getStnm();
                            List<Data> data = null;
                            // sensors.setDataList(new Data());
                            sensors.setHourlyStatisticsDatas(data);
                            sensors.setDataList(data);
                            stations.setSensors(sensors);
                        }
                    }

//                    System.out.println(type+"   das"+StcdList.getValue());
//                    for (int j = 0; j < StcdList.getValue().size(); j++) {
//                        // 获取循环分组后的数据
//                       // System.out.println(StcdList.getValue().get(j));
//                        if (type == StcdList.getValue().get(j).getSensortp()) {
//                            System.out.println("111");
//                            Sensors sensors = new Sensors();
//                            sensors.setSensorName(StcdList.getValue().get(j).getSensornm()); // 传感器名称
//
//                            sensors.setSensorType(StcdList.getValue().get(j).getSensortp()); // 传感器类型
//                            sensors.setDatacode(StcdList.getValue().get(j).getDatacode()); // 传感器取数码
//                            stnm = StcdList.getValue().get(j).getStnm(); // 站名
//                            List<Data> data = null;
//                            // sensors.setDataList(new Data());
//                            sensors.setHourlyStatisticsDatas(data);
//                            sensors.setDataList(data);
//                            stations.setSensors(sensors);
//                            break;
//                        }
//                    }
                }
            }
            stations.setSTCD(StcdList.getKey());
            stations.setStnm(stnm);
            stationsList.add(stations);

        }
//        for (Stations stations : stationsList) {
//            System.out.println(stations);
//        }
        // 3.根据列表逐步循环获取数据 -> 赋值给站网内各自传感器
        for (String s : sensor_T) {

            List<String> datacodes = new ArrayList<String>();
            for (Stations stations : stationsList) {
                System.out.println(stations);
                Sensors sensors = stations.getSensor(s); // 对应传感器信息
                if (sensors != null) {
                    datacodes.add(sensors.getDatacode()); // 获取取数码集合
                }
            }
            // 3.根据列表逐步循环获取数据 -> 赋值给站网内各自传感器
            if (!datacodes.isEmpty()) {
                List<Sensors> sensorDataList = GetDataService.getdata(s, datacodes, StartTM, EndTM, steplength,PID); // 调用获取数据方

                //  System.out.println(sensorDataList);
                if (sensorDataList != null) {
                    String dacode = null;
                    SensorType sentp = null;
                    // 根据传感器类型，取数码，数据类型归类 把dataList数据放入stationList;
                    for (Stations stations : stationsList) {// 循环stationlist集合
                        // 判断数据类型是否与当前数据类型一致；
                        //System.out.println("s1"+stationsList.get(j));
                        if (stations.getSensor(s) != null) {
                            for (int k = 0; k < stations.getSensors().size(); k++) { // 循环每个stations下的sensors集合
                                for (Sensors sensors : sensorDataList) {
                                    dacode = sensors.getDatacode();
                                    sentp = sensors.getSensorType();
                                    if (stations.getSensors().get(k).getDatacode().equals(dacode) && stations.getSensors().get(k).getSensorType() == sentp) {// 判断sensors下的传感器和取数码是否与已有数据里的取数码和传感器相同
                                        // 相同去赋值
                                        stations.getSensors().get(k).setDataList(sensors.getDataList());
                                        stations.getSensors().get(k).setStartTM(sensors.getStartTM());
                                        stations.getSensors().get(k).setEndTM(sensors.getEndTM());
                                        stations.getSensors().get(k).setMinData(sensors.getMinData());
                                        stations.getSensors().get(k).setMaxData(sensors.getMaxData());
                                        stations.getSensors().get(k).setTotalValue(sensors.getTotalValue());
                                        stations.getSensors().get(k).setAvgValue(sensors.getAvgValue());
                                        stations.getSensors().get(k).setDataStartTime(sensors.getDataStartTime());
                                        stations.getSensors().get(k).setDataEndTime(sensors.getDataEndTime());
                                        stations.getSensors().get(k).setMaximumVelocityOfFloodTide(sensors.getMaximumVelocityOfFloodTide());
                                        stations.getSensors().get(k).setMaximumVelocityOfEbbTide(sensors.getMaximumVelocityOfEbbTide());
                                        stations.getSensors().get(k).setHourlyStatisticsDatas(sensors.getHourlyStatisticsDatas());
                                        stations.getSensors().get(k).setRemark(sensors.getRemark());
                                        break; // 找到相同的传感器和取数码后跳出循环
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        jsonResult.setStatusCode(200);
        jsonResult.setSucceeded(true);
        jsonResult.setData(stationsList);

        return jsonResult;

    }
    @ApiOperation("查询实况雨量数据")
    @RequestMapping(value = "Data/GetPPTNData", headers = "Accept=application/json", method = RequestMethod.GET)
    public JsonResult GetPPTNData_GET(@RequestParam String [] Stcds, @RequestParam StepLength steplength, @RequestParam String StartTM, @RequestParam String EndTM) {
        JsonResult jsonResult = new JsonResult();
        PPGetData A = GetDataService.getPPData(Stcds,StartTM,EndTM,steplength);
        jsonResult.setSucceeded(true);
        jsonResult.setStatusCode(200);
        jsonResult.setData(A);
        return jsonResult;

    }
    // 查询最新数据（3合一）
    // 查询最新实况数据
    @ApiOperation("查询最新实况数据")
    @RequestMapping(value = "Data/GetNewestData", method = RequestMethod.GET)
    private JsonResult GetNewEstData_GET(@RequestParam Integer PID, @RequestParam List<String> STCDs,
                                         @RequestParam(required = false) String STTP,
                                         @RequestParam(required = false) List<SensorType> sensorType,
                                         @RequestParam(required = false) String dataTime
                                         ) {
        JsonResult jsonResult = new JsonResult();
        List<NewestData> newestDataList = new ArrayList<NewestData>();

        List<String> sensor_T = GetDataService.getSensor(STTP, sensorType);
        // 取对应站码 传感器表 下的数据
        // 1.根据站点去创建一个站网集合
        //List<BDMS_ST_SENSOR_B> sensor_bList = bDMS_ST_SENSOR_BService.selectBySTCD29(PID, STCDs);
       List<BDMS_ST_SENSOR_B> sensor_bList = bDMS_ST_SENSOR_BService.selectBySTCD(PID, STCDs);

        // 根据站码分组
        Map<String, List<BDMS_ST_SENSOR_B>> StcdListGroup = sensor_bList.stream()
                .collect(Collectors.groupingBy(BDMS_ST_SENSOR_B::getStcd, Collectors.toList()));
        String stnm = null;
        for (Map.Entry<String, List<BDMS_ST_SENSOR_B>> StcdList : StcdListGroup.entrySet()) {
			NewestData newestData = new NewestData();
            if ((sensorType != null && STTP!=null)||(sensorType == null && STTP!=null)) {
                for (int j = 0; j < StcdList.getValue().size(); j++) { // 获取循环分组后的数据
                    Sensors_NewsData sensors_NewsData = new Sensors_NewsData();
                    sensors_NewsData.setSensorName(StcdList.getValue().get(j).getSensornm()); // 传感器名称
                    sensors_NewsData.setSensorType(SensorType.valueOf(StcdList.getValue().get(j).getSensortps())); // 传感器类型
                    sensors_NewsData.setDatacode(StcdList.getValue().get(j).getDatacode()); // 传感器取数码
                   // sensors_NewsData.setWarningTdz(); // 传感器取数码
                    sensors_NewsData.setNewesdata(new Data());
                    // 站名
                    stnm = StcdList.getValue().get(j).getStnm();
                    newestData.setSensors(sensors_NewsData);

                }
			}  else if(sensorType != null){
                for (SensorType type : sensorType) {
                    for (int j = 0; j < StcdList.getValue().size(); j++) { // 获取循环分组后的数据
                        if (type == SensorType.valueOf(StcdList.getValue().get(j).getSensortps())) {
                            Sensors_NewsData sensors_NewsData = new Sensors_NewsData();
                            sensors_NewsData.setSensorName(StcdList.getValue().get(j).getSensornm()); // 传感器名称
                            sensors_NewsData.setSensorType(SensorType.valueOf(StcdList.getValue().get(j).getSensortps())); // 传感器类型
                            sensors_NewsData.setDatacode(StcdList.getValue().get(j).getDatacode()); // 传感器取数码
                            stnm = StcdList.getValue().get(j).getStnm(); // 站名
                            sensors_NewsData.setNewesdata(new Data());
                            newestData.setSensors(sensors_NewsData);
                        }
                    }
                }
            }
            newestData.setStcd(StcdList.getKey());
            newestData.setStnm(stnm);
            newestDataList.add(newestData);
        }
//        // 2.根据站类或者传感器集合创建传感器列表 list
//        boolean res = sensorType != null; // 传感器不为空
//        List<String> sensor_T = new ArrayList<>();
//        if (STTP != null&& res) {// 站类不为空
//            switch (STTP) {
//                case "PP":
//                    sensor_T.add("P");
//                    break;
//                case "ZQ":
//                    sensor_T.add("P");
//                    sensor_T.add("Z");
//                    sensor_T.add("Q");
//                    break;
//                case "RR":
//                    sensor_T.add("P");
//                    sensor_T.add("INQ");
//                    sensor_T.add("OTQ");
//                    sensor_T.add("RZ");
//                    sensor_T.add("W");
//                    break;
//                case "TDZ":
//                    sensor_T.add("TDZ");
//                    break;
//                case "DD":
//                    sensor_T.add("UPZ");
//                    sensor_T.add("DWZ");
//                    sensor_T.add("TGTQ");
//                    break;
//                case "DP":
//                    sensor_T.add("PPUPZ");
//                    sensor_T.add("PPDWZ");
//                    sensor_T.add("PMPQ");
//                    break;
//            }
//        }else if (res) { // 传感器不为空
//            for (SensorType type : sensorType) {
//                sensor_T.add(type.toString());
//            }
//        }else if (STTP !=null){
//            switch (STTP) {
//                case "PP":
//                    sensor_T.add("P");
//                    break;
//                case "ZQ":
//                    sensor_T.add("P");
//                    sensor_T.add("Z");
//                    sensor_T.add("Q");
//                    break;
//                case "RR":
//                    sensor_T.add("P");
//                    sensor_T.add("INQ");
//                    sensor_T.add("OTQ");
//                    sensor_T.add("RZ");
//                    sensor_T.add("W");
//                    break;
//                case "TDZ":
//                    sensor_T.add("TDZ");
//                    break;
//                case "DD":
//                    sensor_T.add("UPZ");
//                    sensor_T.add("DWZ");
//                    sensor_T.add("TGTQ");
//                    break;
//                case "DP":
//                    sensor_T.add("PPUPZ");
//                    sensor_T.add("PPDWZ");
//                    sensor_T.add("PMPQ");
//                    break;
//            }
//        }
        // 3.根据列表逐步循环获取数据 -> 赋值给站网内各自传感器
        for (int i = 0; i < sensor_T.size(); i++) {
            List<String> datacodes = new ArrayList<String>();
            for (int z = 0; z< newestDataList.size(); z++) {
                Sensors_NewsData sensors_NewsData = newestDataList.get(z).getSensor(sensor_T.get(i)); // 对应传感器信息
                if (sensors_NewsData != null) {
                    datacodes.add(sensors_NewsData.getDatacode()); // 获取取数码集合
                }
            }
          if (datacodes.size()>0){
              List<Sensors_NewsData> sensorDataList = GetDataService.getNewesData(sensor_T.get(i), datacodes,PID,dataTime);
              if (sensorDataList != null) {
                  String dacode = null;
                  SensorType sentp = null;
                  // 根据传感器类型，取数码，数据类型归类 把dataList数据放入stationList;
                  for (int j = 0; j < newestDataList.size(); j++) {// 循环stationlist集合
                      if (newestDataList.get(j).getSensors()!=null){
                          for (int k = 0; k < newestDataList.get(j).getSensors().size(); k++) { // 循环每个newestData下的sensors_NewsData集合
                              for (int l = 0; l < sensorDataList.size(); l++) {
                                  dacode = sensorDataList.get(l).getDatacode();
                                  sentp = sensorDataList.get(l).getSensorType();

                                  if (newestDataList.get(j).getSensors().get(k).getDatacode().equals(dacode) && newestDataList.get(j).getSensors().get(k).getSensorType() == sentp) {// 判断sensors_NewsData下的传感器和取数码是否与已有数据里的取数码和传感器相同
                                      // 相同去赋值
                                      newestDataList.get(j).getSensors().get(k).setNewesdata(sensorDataList.get(l).getNewesdata());
                                      if(sensorDataList.get(l).getAlarmlevel()!=null) {
                                          newestDataList.get(j).getSensors().get(k).setAlarmlevel(sensorDataList.get(l).getAlarmlevel());
                                      }
                                      newestDataList.get(j).getSensors().get(k).setDistanceAlert(sensorDataList.get(l).getDistanceAlert());
                                      newestDataList.get(j).getSensors().get(k).setRainStatictisData(sensorDataList.get(l).getRainStatictisData());
                                      break; // 找到相同的传感器和取数码后跳出循环
                                  }
                              }
                          }
                      }
                  }
              }
          }
        }
        jsonResult.setStatusCode(200);
        jsonResult.setSucceeded(true);
        jsonResult.setData(newestDataList);
        return jsonResult;
    }
    // 获取水情预报成果注释表预报信息
    @ApiOperation("获取水情预报成果注释表预报信息")
    @RequestMapping(value = "Data/GetForecastC", method = RequestMethod.GET)
    private JsonResult GetForecastC_GET(@RequestParam List<String> stcds, @RequestParam String unitname,
                                    @RequestParam String plcd,
                                    @RequestParam String startTM, @RequestParam String endTM) {
        JsonResult jsonResult = new JsonResult();

        jsonResult.setStatusCode(200);
        jsonResult.setSucceeded(true);
        jsonResult.setData(GetDataService.GetForecastC(stcds, unitname, plcd, startTM, endTM));
        return jsonResult;

    }

    // 获取水情预报成果数据
    @ApiOperation("获取水情预报成果数据")
    @RequestMapping(value = "Data/GetForecastData", headers = "Accept=application/json", method = RequestMethod.GET)
    private JsonResult GetForecastData_GET(@RequestParam List<String> stcds, @RequestParam String unitname,
                                       @RequestParam String plcd, @RequestParam String fymdh, @RequestParam String iymdh) {
        JsonResult jsonResult = new JsonResult();

        jsonResult.setStatusCode(200);
        jsonResult.setSucceeded(true);
        jsonResult.setData(GetDataService.GetForecastData(stcds, unitname, plcd, fymdh, iymdh));
        return jsonResult;

    }

    //查询实况数据  无站码集合
    @ApiOperation("查询实况数据(无站码集合)")
    @RequestMapping(value = "Data/GetData_NOSTCD", headers = "Accept=application/json", method = RequestMethod.GET)
    public JsonResult GetData_NOSTCD(@RequestParam Integer PID, @RequestParam(required = false) String STTP, @RequestParam(required = false) List<SensorType> sensorType,
                                      @RequestParam StepLength steplength, @RequestParam String StartTM, @RequestParam String EndTM) {
        JsonResult jsonResult = new JsonResult();
        List<Stations> stationsList = new ArrayList<>();
        // 取对应站码 传感器表 下的数据
        // 1.根据站点去创建一个站网集合
        List<BDMS_ST_SENSOR_B> sensor_bList = bDMS_ST_SENSOR_BService.selectByPID(PID);

        // 根据站码分组
        Map<String, List<BDMS_ST_SENSOR_B>> StcdListGroup = sensor_bList.stream()
                .collect(Collectors.groupingBy(BDMS_ST_SENSOR_B::getStcd, Collectors.toList()));
        String stnm = null;
        for (Map.Entry<String, List<BDMS_ST_SENSOR_B>> StcdList : StcdListGroup.entrySet()) {
            Stations stations = new Stations();
            if (STTP != null && sensorType.size() <= 0) {
                for (int j = 0; j < StcdList.getValue().size(); j++) { // 获取循环分组后的数据
                    Sensors sensors = new Sensors();
                    sensors.setSensorName(StcdList.getValue().get(j).getSensornm()); // 传感器名称
                    sensors.setSensorType(SensorType.valueOf(StcdList.getValue().get(j).getSensortps())); // 传感器类型
                    sensors.setDatacode(StcdList.getValue().get(j).getDatacode()); // 传感器取数码
                    stnm = StcdList.getValue().get(j).getStnm(); // 站名
                    stations.setSensors(sensors);
                }
            } else if (sensorType.size() > 0) {
                for (int i = 0; i < sensorType.size(); i++) {
                    for (int j = 0; j < StcdList.getValue().size(); j++) { // 获取循环分组后的数据
                        if (sensorType.get(i) ==SensorType.valueOf(StcdList.getValue().get(j).getSensortps())) {
                            Sensors sensors = new Sensors();
                            sensors.setSensorName(StcdList.getValue().get(j).getSensornm()); // 传感器名称
                            sensors.setSensorType(SensorType.valueOf(StcdList.getValue().get(j).getSensortps())); // 传感器类型
                            sensors.setDatacode(StcdList.getValue().get(j).getDatacode()); // 传感器取数码
                            stnm = StcdList.getValue().get(j).getStnm(); // 站名
                            stations.setSensors(sensors);
                            break;
                        }
                    }
                }
            }
            stations.setSTCD(StcdList.getKey());
            stations.setStnm(stnm);
            stations.setSteplength(steplength);
            stationsList.add(stations);
        }

        // 2.根据站类或者传感器集合创建传感器列表 list
        boolean res = sensorType.size() > 0; // 传感器不为空
        System.out.println(res + "stp");
        List<String> sensor_T = new ArrayList<>();
        if (res) { // 传感器不为空
            for (int i = 0; i < sensorType.size(); i++) {
                sensor_T.add(sensorType.get(i).toString());
            }
        } else if (STTP != null) {// 站类不为空
            switch (STTP) {
                case "PP":
                    sensor_T.add("P");
                    break;
                case "ZQ":
                    sensor_T.add("P");
                    sensor_T.add("Z");
                    sensor_T.add("Q");
                    break;
                case "RR":
                    sensor_T.add("P");
                    sensor_T.add("INQ");
                    sensor_T.add("OTQ");
                    sensor_T.add("RZ");
                    sensor_T.add("W");
                    break;
                case "TDZ":
                    sensor_T.add("TDZ");
                    break;
                case "DD":
                    sensor_T.add("UPZ");
                    sensor_T.add("DWZ");
                    sensor_T.add("TGTQ");
                    break;
                case "DP":
                    sensor_T.add("PPUPZ");
                    sensor_T.add("PPDWZ");
                    sensor_T.add("PMPQ");
                    break;
            }
        }
        // 3.根据列表逐步循环获取数据 -> 赋值给站网内各自传感器
        for (int i = 0; i < sensor_T.size(); i++) {
            List<String> datacodes = new ArrayList<>();
            for (int j = 0; j < stationsList.size(); j++) {
                Sensors sensors = stationsList.get(j).getSensor(sensor_T.get(i)); // 对应传感器信息
                if (sensors != null) {
                    datacodes.add(sensors.getDatacode()); // 获取取数码集合
                }
            }
            List<Sensors> sensorDataList = GetDataService.getdata(sensor_T.get(i), datacodes, StartTM, EndTM,
                    steplength,PID); // 调用获取数据方法
            if (sensorDataList != null) {

                String dacode = null;
                SensorType sentp = null;
                // 根据传感器类型，取数码，数据类型归类 把dataList数据放入stationList;
                for (int j = 0; j < stationsList.size(); j++) {// 循环stationlist集合

                    if (stationsList.get(j).getSteplength() == steplength) {// 判断数据类型是否与当前数据类型一致；
                        for (int k = 0; k < stationsList.get(j).getSensors().size(); k++) { // 循环每个stations下的sensors集合
                            for (int l = 0; l < sensorDataList.size(); l++) {
                                dacode = sensorDataList.get(l).getDatacode();
                                sentp = sensorDataList.get(l).getSensorType();
                                if (stationsList.get(j).getSensors().get(k).getDatacode().equals(dacode) && stationsList.get(j).getSensors().get(k).getSensorType() == sentp) {// 判断sensors下的传感器和取数码是否与已有数据里的取数码和传感器相同
                                    // 相同去赋值
                                    stationsList.get(j).getSensors().get(k).setDataList(sensorDataList.get(l).getDataList());
                                    stationsList.get(j).getSensors().get(k).setStartTM(sensorDataList.get(l).getStartTM());
                                    stationsList.get(j).getSensors().get(k).setEndTM(sensorDataList.get(l).getEndTM());
                                    stationsList.get(j).getSensors().get(k).setMinData(sensorDataList.get(l).getMinData());
                                    stationsList.get(j).getSensors().get(k).setMaxData(sensorDataList.get(l).getMaxData());
                                    stationsList.get(j).getSensors().get(k).setTotalValue(sensorDataList.get(l).getTotalValue());
                                    stationsList.get(j).getSensors().get(k).setAvgValue(sensorDataList.get(l).getAvgValue());
                                    break; // 找到相同的传感器和取数码后跳出循环
                                }
                            }
                        }

                    }
                }
            }
        }
        jsonResult.setStatusCode(200);
        jsonResult.setSucceeded(true);
        jsonResult.setData(stationsList);

        return jsonResult;

    }
    //查询水库和雨量最新数据（日期条件）
    @ApiOperation("查询水库和雨量最新实况数据")
    @RequestMapping(value = "Data/GetPRNewestData", method = RequestMethod.GET)
    private JsonResult GetNewEstData_GET(@RequestParam Integer PID, @RequestParam(required = false) String STTP, @RequestParam(required = false) List<SensorType> sensorType,
                                         @RequestParam String Date) {
        JsonResult jsonResult = new JsonResult();
        List<Sensors_PR> newestDataList = new ArrayList<Sensors_PR>();
        // 取对应站码 传感器表 下的数据
        // 1.根据站点去创建一个站网集合

        List<BDMS_ST_SENSOR_B> sensor_bList = bDMS_ST_SENSOR_BService.selectByPid(PID);
        // 根据站码分组
        Map<String, List<BDMS_ST_SENSOR_B>> StcdListGroup = sensor_bList.stream()
                .collect(Collectors.groupingBy(BDMS_ST_SENSOR_B::getStcd, Collectors.toList()));
        String stnm = null;
        for (Map.Entry<String, List<BDMS_ST_SENSOR_B>> StcdList : StcdListGroup.entrySet()) {
            Sensors_PR newestData = new Sensors_PR();
            boolean res1 = sensorType == null || sensorType.size() <= 0;
            if ((sensorType != null && STTP!=null)||(sensorType == null && STTP!=null)) {
                for (int j = 0; j < StcdList.getValue().size(); j++) { // 获取循环分组后的数据
                    PRData sensors_NewsData = new PRData();
                    sensors_NewsData.setSensorName(StcdList.getValue().get(j).getSensornm()); // 传感器名称
                    sensors_NewsData.setSensorType(SensorType.valueOf(StcdList.getValue().get(j).getSensortps())); // 传感器类型
                    sensors_NewsData.setDatacode(StcdList.getValue().get(j).getDatacode()); // 传感器取数码
                    stnm = StcdList.getValue().get(j).getStnm(); // 站名
                    newestData.setSensors(sensors_NewsData);

                }
            }  else if(sensorType != null){
                for (int i = 0; i < sensorType.size(); i++) {
                    for (int j = 0; j < StcdList.getValue().size(); j++) { // 获取循环分组后的数据
                        if (sensorType.get(i) == SensorType.valueOf(StcdList.getValue().get(j).getSensortps())) {
                            PRData sensors_NewsData = new PRData();
                            sensors_NewsData.setSensorName(StcdList.getValue().get(j).getSensornm()); // 传感器名称
                            sensors_NewsData.setSensorType(SensorType.valueOf(StcdList.getValue().get(j).getSensortps())); // 传感器类型
                            sensors_NewsData.setDatacode(StcdList.getValue().get(j).getDatacode()); // 传感器取数码
                            stnm = StcdList.getValue().get(j).getStnm(); // 站名
                            newestData.setSensors(sensors_NewsData);
                            break;
                        }
                    }
                }
            }

            newestData.setStcd(StcdList.getKey());
            newestData.setStnm(stnm);
            newestDataList.add(newestData);
        }

        // 2.根据站类或者传感器集合创建传感器列表 list
        boolean res = sensorType != null; // 传感器不为空
        List<String> sensor_T = new ArrayList<>();
        if (STTP != null&& res) {// 站类不为空
            switch (STTP) {
                case "PP":
                    sensor_T.add("P");
                    break;
                case "RR":
                    sensor_T.add("P");
                    sensor_T.add("RZ");
                    sensor_T.add("W");
                    break;
            }
        }else if (res) { // 传感器不为空
            for (SensorType type : sensorType) {
                sensor_T.add(type.toString());
            }
        }else if (STTP !=null){
            switch (STTP) {
                case "PP":
                    sensor_T.add("P");
                    break;
                case "RR":
                    sensor_T.add("P");
                    sensor_T.add("RZ");
                    sensor_T.add("W");
                    break;
            }
        }
        // 3.根据列表逐步循环获取数据 -> 赋值给站网内各自传感器
        for (int i = 0; i < sensor_T.size(); i++) {
            List<String> datacodes = new ArrayList<String>();
            for (int z = 0; z< newestDataList.size(); z++) {
                PRData pRData = newestDataList.get(z).getSensor(sensor_T.get(i)); // 对应传感器信息
                if (pRData != null) {
                    datacodes.add(pRData.getDatacode()); // 获取取数码集合
                }
            }
            if (datacodes.size()>0){
                List<PRData> sensorDataList = GetDataService.getpptnrsvrNewesData(sensor_T.get(i), datacodes,Date);
                if (sensorDataList != null && newestDataList!=null) {
                    String dacode = null;
                    SensorType sentp = null;
                    // 根据传感器类型，取数码，数据类型归类 把dataList数据放入stationList;
                    for (int j = 0; j < newestDataList.size(); j++) {// 循环stationlist集合
                        if (newestDataList.get(j).getSensors()!=null){
                            for (int k = 0; k < newestDataList.get(j).getSensors().size(); k++) { // 循环每个sensors_PR下的pRData集合
                                for (int l = 0; l < sensorDataList.size(); l++) {
                                    dacode = sensorDataList.get(l).getDatacode();
                                    sentp = sensorDataList.get(l).getSensorType();
                                    if (newestDataList.get(j).getSensors().get(k).getDatacode().equals(dacode) && newestDataList.get(j).getSensors().get(k).getSensorType() == sentp) {// 判断pRData下的传感器和取数码是否与已有数据里的取数码和传感器相同
                                        // 相同去赋值
                                        newestDataList.get(j).getSensors().get(k).setSensorName(sensorDataList.get(l).getSensorName());
                                        newestDataList.get(j).getSensors().get(k).setSensorType(sensorDataList.get(l).getSensorType());
                                        newestDataList.get(j).getSensors().get(k).setEightLevel(sensorDataList.get(l).getEightLevel());
                                        newestDataList.get(j).getSensors().get(k).setNewesdata(sensorDataList.get(l).getNewesdata());
                                        newestDataList.get(j).getSensors().get(k).setPeakLevel(sensorDataList.get(l).getPeakLevel());
                                        newestDataList.get(j).getSensors().get(k).setYesterdayRainDate(sensorDataList.get(l).getYesterdayRainDate());
                                        break; // 找到相同的传感器和取数码后跳出循环
                                    }
                                }
                            }
                        }

                    }
                }
            }

        }

        jsonResult.setStatusCode(200);
        jsonResult.setSucceeded(true);
        jsonResult.setData(newestDataList);

        return jsonResult;

    }


    //报表业务
    //单站雨量日报表
    @ApiOperation("单站雨量日报表")
    @RequestMapping(value = "Report/GetSingleStationDailyRainfall", method = RequestMethod.GET)
    private JsonResult GetSingleStationDailyRainfall(@RequestParam String Stcd,@RequestParam String Date) {
        HourlyRainfall    hourlyRainfall=   GetDataService.getSingleStationDailyRainfall(Stcd,Date);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setStatusCode(200);
        jsonResult.setSucceeded(true);
        jsonResult.setData(hourlyRainfall);
        return jsonResult;
    }

    //GetSingleStationMonthRainfall
    //单站雨量月报表
    @ApiOperation("单站雨量月报表")
    @RequestMapping(value = "Report/GetSingleStationMonthRainfall", method = RequestMethod.GET)
    private JsonResult GetSingleStationMonthRainfall(@RequestParam String Stcd,@RequestParam String Year) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.setStatusCode(200);
        jsonResult.setSucceeded(true);
        jsonResult.setData(GetDataService.getSingleStationMonthRainfall(Stcd,Year));
        return jsonResult;
    }

    //多站报表
    //多站逐时报表    GetMultiStationDailyRainfall
    @ApiOperation("多站雨量日报表")
    @RequestMapping(value = "Report/GetMultiStationDailyRainfall", method = RequestMethod.GET)
    private JsonResult GetMultiStationDailyRainfall(@RequestParam String[] Stcds,@RequestParam String Date) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.setStatusCode(200);
        jsonResult.setSucceeded(true);
        jsonResult.setData(GetDataService.getMultiStationRainfall("HH:mm",24,"00:00",GetDataService.getMultiStationDailyRainfall(Stcds,Date)));
        return jsonResult;
    }


    //多站逐日报表    GetMultiStationMonthRainfall
    @ApiOperation("多站雨量月报表")
    @RequestMapping(value = "Report/GetMultiStationMonthRainfall", method = RequestMethod.GET)
    private JsonResult GetMultiStationMonthRainfall(@RequestParam String[] Stcds,@RequestParam String Month) {
        int year = Integer.parseInt(Month.substring(0,4));
        String month =Month.substring(5,7);
        int num = 0;
        //年份能被4整除，并且不能被100整除并且使用&&（and）
        // 年份或者能够被400整除
        switch (month) {
            case "01":
            case "03":
            case "05":
            case "07":
            case "08":
            case "10":
            case "12":
                num=31;
                
                break;
            case "04":
            case "06":
            case "09":
            case "11": 
                 num =30;
                break;
            case "02":
                if(year%4==0&&year%100!=0||year%400==0){
                    num =29;
                }else{
                    num =28;
                }
                break;
        }

        JsonResult jsonResult = new JsonResult();
        jsonResult.setStatusCode(200);
        jsonResult.setSucceeded(true);
        jsonResult.setData(GetDataService.getMultiStationRainfall("dd",num,"01",GetDataService.getMultiStationMonthRainfall(Stcds,Month)));
        return jsonResult;
    }


    //多站逐月报表    GetMultiStationMonthRainfall
    @ApiOperation("多站雨量年报表")
    @RequestMapping(value = "Report/GetMultiStationYearRainfall", method = RequestMethod.GET)
    private JsonResult GetMultiStationYearRainfall(@RequestParam String[] Stcds,@RequestParam String Year) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.setStatusCode(200);
        jsonResult.setSucceeded(true);
        jsonResult.setData(GetDataService.getMultiStationRainfall("MM",12,"01", GetDataService.getMultiStationYearRainfall(Stcds,Year)));
        return jsonResult;
    }
    //GetStationDailyWaterLevel

    //水位日逐时报表
    @ApiOperation("水位日逐时报表")
    @RequestMapping(value = "Report/GetStationDailyWaterLevel", method = RequestMethod.GET)
    //@Transactional(transactionManager = "rhdbDataSource")
    public JsonResult GetStationDailyWaterLevel(@RequestParam String Stcd,@RequestParam String Date) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.setStatusCode(200);
        jsonResult.setSucceeded(true);
        jsonResult.setData(GetDataService.getStationDailyWaterLevel(Stcd,Date));
        return jsonResult;
    }

    //水位月逐日报表
    @ApiOperation("水位月逐日报表")
    @RequestMapping(value = "Report/GetStationMonthWaterLevel", method = RequestMethod.GET)
    public JsonResult GetStationMonthWaterLevel(@RequestParam String Stcd,@RequestParam String Year) {
        JsonResult jsonResult = new JsonResult();
        MonthWaterLevel monthWaterLevel =  GetDataService.getStationMonthWaterLevel(Stcd,Year);

        jsonResult.setStatusCode(200);
        jsonResult.setSucceeded(true);
        jsonResult.setData(monthWaterLevel);
        return jsonResult;
    }


    //获取逐时天文潮数据
    @ApiOperation("查询实测潮位数据")
    @RequestMapping(value = "Report/GetTide", headers = "Accept=application/json", method = RequestMethod.POST)
    public JsonResult getTide(@RequestBody TideParame tideParame){
        JsonResult jsonResult = new JsonResult();
        jsonResult.setStatusCode(200);
        jsonResult.setSucceeded(true);
        LocalDateTime dateTime1 = toTimeUtil.getDateTime(tideParame.getStartTime());
        LocalDateTime dateTime2 = toTimeUtil.getDateTime(tideParame.getEndTime());
        if (dateTime1.compareTo(dateTime2) <= 0) {
            List<TwcTide> tides = GetDataService.getTide(tideParame.getStationCodes(), tideParame.getStartTime(),tideParame.getEndTime(),tideParame.getHourData());
            if (tides.size()>0){
                jsonResult.setData(tides);
                jsonResult.setRemark("请求成功");
            }else {
                jsonResult.setRemark("未查询到此站点该时间段内的数据");
            }
        } else  {
            jsonResult.setRemark("开始时间大于结束时间，请更换参数值");
        }
        return jsonResult;
    }
    //获取逐时天文潮数据
    @ApiOperation("查询实测高低潮数据")
    @RequestMapping(value = "Report/GetHLTide", headers = "Accept=application/json", method = RequestMethod.POST)
    public JsonResult getHLTide(@RequestBody TideParame tideParame){
        JsonResult jsonResult = new JsonResult();
        jsonResult.setStatusCode(200);
        jsonResult.setSucceeded(true);
        LocalDateTime dateTime1 = toTimeUtil.getDateTime(tideParame.getStartTime());
        LocalDateTime dateTime2 = toTimeUtil.getDateTime(tideParame.getEndTime());
        if (dateTime1.compareTo(dateTime2) <= 0) {
            List<TwcTide> tides =  GetDataService.getHLTide(tideParame.getStationCodes(), tideParame.getStartTime(),tideParame.getEndTime());   if (tides.size()>0){
                jsonResult.setData(tides);
                jsonResult.setRemark("请求成功");
            }else {
                jsonResult.setRemark("未查询到此站点该时间段内的数据");
            }
        } else  {
            jsonResult.setRemark("开始时间大于结束时间，请更换参数值");
        }

        return jsonResult;
    }





}
